package com.decent.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.decent.entity.Product;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * @author decent
 */
@Mapper
public interface ProductMapper  extends BaseMapper<Product> {

    /**
     * 根据主键更新
     * @param id 产品id
     * @param count 购买数量
     * @return 影响行数
     */
    @Update("update product\n" +
            "set\n" +
            "  stock = (stock - #{count})\n" +
            "where id = #{id}\n" +
            "    AND (stock - #{count}) >= 0")
    int updateByPrimaryKey(@Param("id") Long id, @Param("count") Integer count);

}